/*
 * Copyright (C) 2010 Enrique Lara (enrique.posix2.lara@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package app.tellwmata;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import android.view.View.OnClickListener;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import static app.tellwmata.Constants.TYPE_BUS;
import static app.tellwmata.Constants.TYPE_METRO;
import static app.tellwmata.Constants.TYPE_PARATRANSIT;

import app.tellwmata.model.Agency;
import app.tellwmata.model.App;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Metro extends Activity implements OnItemClickListener {
    private static final String TAG = "AllAlarms";
    public static final int MENU_ITEM_SETTING = Menu.FIRST;
    public static final int MENU_ITEM_PROFILE = Menu.FIRST + 1;
    public static final int MENU_ITEM_ABOUT = Menu.FIRST + 2;
    private ListView allForms = null;
    private Agency agency = null;
    private App app = null;

    @Override
    public final void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT);

        agency = Agency.getInstance(this);
        app = new App(this);

        setContentView(R.layout.forms);

        final int routeType = getRouteType();
        final SimpleAdapter adapter = new SimpleAdapter(this, getData(routeType), android.R.layout.simple_list_item_1, new String[] {
                                                            "title"
                                                        }, new int[] {
                                                            android.R.id.text1
                                                        });

        allForms = (ListView) findViewById(R.id.all_forms);
        allForms.setAdapter(adapter);
        allForms.setOnItemClickListener(this);

        Button b = (Button) findViewById(R.id.metro);
        b.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    app.gotoMetro();
                }
            });
        b.setEnabled(routeType != TYPE_METRO);

        b = (Button) findViewById(R.id.buses);
        b.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    app.gotoBuses();
                }
            });
        b.setEnabled(routeType != TYPE_BUS);

        b = (Button) findViewById(R.id.paratransit);
        b.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    app.gotoParatransit();
                }
            });
        b.setEnabled(routeType != TYPE_PARATRANSIT);

        switch (routeType) {
            case TYPE_BUS:
                setTitle(getString(R.string.buses) + " - " + getString(R.string.app_name));

                break;

            case TYPE_METRO:
                setTitle(getString(R.string.metro) + " - " + getString(R.string.app_name));

                break;

            case TYPE_PARATRANSIT:
                setTitle(getString(R.string.paratransit) + " - " + getString(R.string.app_name));

                break;

            default:
                setTitle(getString(R.string.app_name));

                break;
        }
    }

    private List<?extends Map<String, ?>> getData(int routeType) {
        List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();

        final ArrayList<HashMap<String, String>> forms = agency.getForms(routeType);

        for (HashMap<String, String> form : forms) {
            final String formName = form.get(Constants.FormFields.FORM_NAME);
            final String title = form.get("title");

            final Intent intent = new Intent(Constants.ACTION_EDIT);
            intent.putExtra(Constants.FormFields.FORM_NAME, formName);
            intent.putExtra("title", title);
            intent.putExtra(Constants.FormFields.ROUTE_TYPE, routeType);

            final HashMap<String, Object> temp = new HashMap<String, Object>();
            temp.put("title", title);
            temp.put("intent", intent);
            data.add(temp);
        }

        return data;
    }

    protected int getRouteType() {
        final Intent intent = getIntent();

        if (intent != null) {
            final Bundle extras = intent.getExtras();

            if ((extras != null) && (extras.containsKey(Constants.FormFields.ROUTE_TYPE))) {
                return extras.getInt(Constants.FormFields.ROUTE_TYPE);
            }
        }

        return Settings.getHomeType(this);
    }

    @Override
    public final boolean onCreateOptionsMenu(final Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0, MENU_ITEM_ABOUT, 0, R.string.about).setShortcut('6', 'i').setIcon(android.R.drawable.ic_menu_info_details);
        menu.add(0, MENU_ITEM_PROFILE, 1, R.string.profile).setShortcut('3', 'p').setIcon(android.R.drawable.ic_menu_add);
        menu.add(0, MENU_ITEM_SETTING, 2, R.string.settings).setShortcut('9', 's').setIcon(android.R.drawable.ic_menu_preferences);

        return true;
    }

    @Override
    public final boolean onOptionsItemSelected(final MenuItem item) {
        switch (item.getItemId()) {
            case MENU_ITEM_SETTING:
                return app.gotoSettings();

            case MENU_ITEM_ABOUT:
                return app.gotoAbout();

            case MENU_ITEM_PROFILE:
                return app.gotoProfile();
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onItemClick(AdapterView<?> av, View v, int position, long id) {
        Map map = (Map) av.getItemAtPosition(position);
        Intent intent = (Intent) map.get("intent");
        startActivity(intent);
    }
}
